# -*- coding: UTF-8 -*-


class Unit(object):

    def __init__(self, json_dict):

        self.name = json_dict['name']
        self.idInRule = json_dict['idInRule']
        self.parameter = json_dict['parameter']
        self.uuid = json_dict['id']
        self.terminal = json_dict['terminal']
        self.idInGraph = -1


class Rule(object):

    def __init__(self, name, units, expressions, is_start_rule):

        self.name = name
        self.units = []
        self.idInGraph = -1
        self.expressions = []

        for unit in units:
            if unit['builtin']:
                continue
            obj = Unit(unit)
            self.units.append(obj)

        for expJson in expressions:
            expression = []
            for item in expJson:
                unit = Unit(item)
                expression.append(unit)
            self.expressions.append(expression)

        self.isStartRule = is_start_rule


class FstEdge(object):

    def __init__(self, id_in_graph: object, score: object, text: object) -> object:

        self.dst = id_in_graph
        self.score = score
        self.text = text


class GraphNode(object):

    def __init__(self, uuid_in):

        self.uuid = uuid_in
        self.edges = {}

    def make_edge(self, edge):

        if edge.dst in self.edges:
            return
        self.edges[edge.dst] = edge

    def contains_edge(self, dst_id):

        return dst_id in self.edges


